import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:memo/model/task.dart';
import 'package:memo/utils/toast.dart';
import 'package:memo/service/task_dao.dart';

class DetailPage extends StatefulWidget {
  TaskDao taskDao;
  Task task;
  DetailPage(this.taskDao, this.task);

  @override
  _DetailPageState createState() => _DetailPageState();
}

class _DetailPageState extends State<DetailPage> {
  TextEditingController contentController = TextEditingController();
  Task tmpTask;

  @override
  void initState() {
    super.initState();
    tmpTask = Task(
      id: widget.task.id,
      content: widget.task.content,
      endTime: widget.task.endTime,
      isFinish: widget.task.isFinish,
    );
    setState(() {
      contentController.text = tmpTask.content;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("详情"),
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.save),
            onPressed: (){
              tmpTask.content = contentController.text;
              widget.taskDao.updateTask(tmpTask);
              Toast.toast(context,msg: "修改成功！",position: ToastPostion.top);
            },
          ),
          SizedBox(width: 15,)
        ],
      ),
      body: Container(
        margin: EdgeInsets.all(6),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            SizedBox(height: 8,),
            _buildContent(),
            SizedBox(height: 8,),
            _buildEndTime(context),
            Divider(height:10.0,indent:0.0,color: Colors.grey[600]),
            _buildIsFinsh(),
          ],
        ),
      ),
    );
  }

  Widget _buildContent(){
    return TextField(
      controller: contentController,
      keyboardType: TextInputType.multiline,
      maxLines: 5,
      style: TextStyle(fontSize: 20),
      decoration: InputDecoration(
        labelText: "内容",
        border: OutlineInputBorder(),
      ),
    );
  }

  Widget _buildEndTime(BuildContext context){
    return GestureDetector(
      onTap: (){
        showDatePicker(
          locale: Locale('zh'),
          context: context,
          initialDate: tmpTask.endTime,
          firstDate: DateTime(2012),
          lastDate: DateTime(2050),
        ).then((value) {
          if(value!=null){
            setState(() {
              tmpTask.endTime = value;
            });
          }
        });
      },
      child: Row(
        children: <Widget>[
          Text(
            " 截止时间:  ${tmpTask.endTimeStr}   ",
            style: TextStyle(
              fontSize: 20,
            ),
          ),
          Icon(Icons.date_range,color: Colors.blue[500],)
        ],
      ),
    );
  }

  Widget _buildIsFinsh(){
    return Row(
      mainAxisSize: MainAxisSize.min,
      children: <Widget>[
        Text(" 是否已完成",style: TextStyle(fontSize: 18),),
        Switch(
          value: tmpTask.isFinish,//当前状态
          onChanged:(value){
            //重新构建页面
            setState(() {
              tmpTask.isFinish=value;
            });
          },
        ),
      ],
    );
  }
}
